# -*- encoding: utf-8 -*-
"""
Edited at 2018-6-12 by Stone
    1. Add CrossRoad.laneGuides and its visual and  invisual method.
"""
__metaclass__ = type
from Shapes import *
from visual import *

# Type definition
pos = vector  # to convenient some pos calculation

# Data

# Functions


# Classes
class Car(VehicleShape):
    def __init__(self, model="car"):
        self.__pos = None
        self.__v = None
        self.__a = None
        self.__w = None  # omega, angular velocity

    def get_pos(self):
        return self.__pos

    def construct_shape(self, model):
        if model == "car":pass
        elif model == "bus":pass

    def rule_keep_distance(self):pass

    def rule_keep_velocity(self):pass

    def drive(self):
        self.rule_keep_distance()
        "..."


class CrossRoad(CrossRoadShape):

    def __init__(self, frame):
        self.f = frame
        CrossRoadShape.__init__(self, self.f)

        self.ifShow_LaneGuides = True
        self.laneGuides = {'x': [], '-x': [], 'y': [], '-y': [], 'visual': []}  # used to guide the vehicles
        self.start = {'x': [], '-x': [], 'y': [], '-y': [], 'visual': []}  # where the vehicles start

        self.init_laneGuides()
        if self.ifShow_LaneGuides: self.visual_laneGuides()
        self.construct_shape()

    def init_laneGuides(self):
        def r(v): return -v[1], v[0], v[2]                          # rotate 90 degree
        num_of_lanes = 4
        gap = self.width / num_of_lanes
        direction = vector(1,0,0)
        # not clever enough, so manually specify some values intelligently
        k=2
        for i in range(1, num_of_lanes + 1):
            self.start['x'].append(( -k * self.length,  i * self.width / (num_of_lanes + 1), 0))
            self.start['-x'].append(( k * self.length, -i * self.width / (num_of_lanes + 1), 0))
            self.start['y'].append((  i * self.width / (num_of_lanes + 1), -k * self.length, 0))
            self.start['-y'].append((-i * self.width / (num_of_lanes + 1),  k * self.length, 0))

            #the lanes that go straight
            self.laneGuides[ 'x'].append([( k*self.length, i*self.width/ (num_of_lanes+1), 0)])  # the guide point
            self.laneGuides['-x'].append([(-k*self.length,-i*self.width/ (num_of_lanes+1), 0)])  # -x direction
            self.laneGuides[ 'y'].append([(i*self.width/ (num_of_lanes+1),  k*self.length, 0)])  # y direction, too
            self.laneGuides['-y'].append([(-i*self.width/(num_of_lanes+1), -k*self.length, 0)])

        # the fifth lanes that turn right
        # the middle guide points
        self.laneGuides['x'].append([(self.laneGuides['-y'][num_of_lanes - 1][0][0], i * self.width / (num_of_lanes + 1), 0)])
        self.laneGuides['-x'].append([(self.laneGuides[ 'y'][num_of_lanes - 1][0][0], -i * self.width / (num_of_lanes + 1), 0)])
        self.laneGuides['y'].append([(i * self.width / (num_of_lanes + 1), self.laneGuides[ 'x'][num_of_lanes - 1][0][1], 0)])
        self.laneGuides['-y'].append([(-i * self.width / (num_of_lanes + 1), self.laneGuides['-x'][num_of_lanes - 1][0][1], 0)])
        k = 2
        self.start['x'].append((-k * self.length, i * self.width / (num_of_lanes + 1), 0))
        self.start['-x'].append((k * self.length, -i * self.width / (num_of_lanes + 1), 0))
        self.start['y'].append((i * self.width / (num_of_lanes + 1), -k * self.length, 0))
        self.start['-y'].append((-i * self.width / (num_of_lanes + 1), k * self.length, 0))
        # the end guide points
        self.laneGuides[ 'x'][-1].append((self.laneGuides['-y'][num_of_lanes - 1][0][0], -k*self.length, 0))
        self.laneGuides['-y'][-1].append((-k*self.length, self.laneGuides['-x'][num_of_lanes - 1][0][1], 0))
        self.laneGuides['-x'][-1].append((self.laneGuides[ 'y'][num_of_lanes - 1][0][0],  k*self.length, 0))
        self.laneGuides[ 'y'][-1].append(( k*self.length, self.laneGuides[ 'x'][num_of_lanes - 1][0][1], 0))
        print self.laneGuides

    def invisual_laneGuides(self):
        for obj in self.laneGuides['visual']:
            obj.visible = False

    def visual_laneGuides(self):
        if self.laneGuides['visual'] == []:  # time to initialize
            for i in self.laneGuides:
                if i == 'visual': continue
                for j in range(len(self.laneGuides[i])):
                    self.laneGuides['visual'].append(curve(pos=[self.start[i][j]] + self.laneGuides[i][j]))
        for obj in self.laneGuides['visual']:
            obj.visible = True


# Functions
def half(x):
    return x / 2.0


# Initialization
scene = display(title=u"十字路口模拟器2018 - by teleM".encode("gbk"))
scene.fov = pi/8
scene.forward = (0, -1, -1)
scene.up = (0, 0, 1)
crd = CrossRoad(frame())


# Main Loop
if __name__ == '__main__':
    while 1:
        rate(1)
